#include "Effect.h"
const int Effect::FrameCount = 3;
const int Effect::TicksPerFrame = 100;

Effect::Effect(const string &file) :
  mpSurface(0), frameCounter(0), playCounter(0) {
  mpSurface = IMG_Load(file.c_str());
  if (!mpSurface) {
    cerr << "Effect error: " << SDL_GetError() << " Unable to load effect image: "  << file.c_str() << endl;
    exit(200711291);
  }
  frameTime = lastFrameTime = SDL_GetTicks();
}


Effect::~Effect()
{
  if (mpSurface) SDL_FreeSurface(mpSurface);
}

SDL_Surface *Effect::getSurface() {
  return mpSurface;
}

const int Effect::getFrameCounter() const {
  return frameCounter;
}

const int Effect::getPlayCounter() const {
  return playCounter;
}

void Effect::setFrameCounter(const int f) {
  frameCounter = f % FrameCount;
  playCounter = 0;
}

void Effect::nextFrame() {
  if (frameCounter++ > FrameCount) {
    frameCounter = 0;
    playCounter++;
  }
}

void Effect::animNextFrame() {
  frameTime = SDL_GetTicks();
  if (frameTime - lastFrameTime > TicksPerFrame) {
    // time to advance a frame
    if (frameCounter++ > FrameCount) {
      frameCounter = 0;
      playCounter++;
    }
    lastFrameTime = frameTime;
  }
}

